package com.ruoyi.web.controller.lims;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lims.domain.LimsMissionIssued;
import com.ruoyi.lims.domain.LimsMissionIssuedChild;
import com.ruoyi.lims.service.ILimsMissionIssuedChildService;
import com.ruoyi.lims.service.ILimsMissionIssuedService;
import com.ruoyi.lims.service.ILimsOperateComputerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 项目Controller
 * 
 * @author ruoyi
 * @date 2023-08-30
 */
@RestController
@RequestMapping("/lims/mission/issued")
public class LimsMissionIssuedController extends BaseController
{
    @Autowired
    private ILimsMissionIssuedService limsMissionIssuedService;
    @Autowired
    private ILimsMissionIssuedChildService limsMissionIssuedChildService;
    @Autowired
    private ILimsOperateComputerInfoService limsOperateComputerInfoService;
    /**
     * 查询项目列表
     */
    @PreAuthorize("@ss.hasPermi('lims:mission:issued:list')")
    @GetMapping("/list")
    public TableDataInfo list(LimsMissionIssued limsMissionIssued)
    {
        startPage();
        List<LimsMissionIssued> list = limsMissionIssuedService.queryList(limsMissionIssued);
        return getDataTable(list);
    }

    /**
     * 导出项目列表
     */
    @PreAuthorize("@ss.hasPermi('lims:mission:issued:export')")
    @Log(title = "项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LimsMissionIssued limsMissionIssued)
    {
        List<LimsMissionIssued> list = limsMissionIssuedService.queryList(limsMissionIssued);
        ExcelUtil<LimsMissionIssued> util = new ExcelUtil<LimsMissionIssued>(LimsMissionIssued.class);
        util.exportExcel(response, list, "项目数据");
    }

    /**
     * 获取项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('lims:mission:issued:query')")
    @GetMapping(value = "/{miId}")
    public AjaxResult getInfo(@PathVariable("miId") Integer miId)
    {
        return AjaxResult.success(limsMissionIssuedService.getById(miId));
    }

    /**
     * 新增项目
     */
    @PreAuthorize("@ss.hasPermi('lims:mission:issued:add')")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LimsMissionIssued limsMissionIssued)
    {
        int period= (int)limsMissionIssuedService.getCycleCount(limsMissionIssued);
        Date startTime= DateUtils.getNowDate();
        Date endTime=DateUtils.addHours(startTime,period);
        limsMissionIssued.setBeginTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,startTime));
        limsMissionIssued.setEndTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,endTime));
        limsMissionIssued.setCreateBy(getUserId().toString());
        limsMissionIssued.setIssuedNum(limsMissionIssuedService.getIssuedNum(limsMissionIssued));
        return toAjax(limsMissionIssuedService.save(limsMissionIssued));
    }

    /**
     * 修改项目
     */
    @PreAuthorize("@ss.hasPermi('lims:mission:issued:edit')")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LimsMissionIssued limsMissionIssued)
    {
        if(limsMissionIssued.getSampleNum()!=null){
            int period= (int)limsMissionIssuedService.getCycleCount(limsMissionIssued);
            Date startTime= DateUtils.getNowDate();
            Date endTime=DateUtils.addHours(startTime,period);
            limsMissionIssued.setBeginTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,startTime));
            limsMissionIssued.setEndTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,endTime));
        }
        if(limsMissionIssued.getProgramType()!=null){
            limsMissionIssued.setIssuedNum(limsMissionIssuedService.getIssuedNum(limsMissionIssued));
        }
        return toAjax(limsMissionIssuedService.updateById(limsMissionIssued));
    }
    /**
     * 分配调度人
     */
    @PreAuthorize("@ss.hasPermi('lims:mission:issued:allot')")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PostMapping("/allot")
    public AjaxResult allot(@RequestBody LimsMissionIssued limsMissionIssued)
    {
        if(limsMissionIssued.getAttachment()==null){
            throw new ServiceException("协议名称 "+limsMissionIssued.getProtocolName()+"缺少附件，请上传<br/>");
        }
        limsMissionIssued.setStatus("1");

        LimsMissionIssuedChild child=new LimsMissionIssuedChild();

        LambdaQueryWrapper<LimsMissionIssuedChild> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(LimsMissionIssuedChild::getParentId,limsMissionIssued.getMiId());
        LimsMissionIssuedChild search=limsMissionIssuedChildService.getOne(queryWrapper);
           if(search!=null){
               child=search;
           }
        BeanUtils.copyProperties(limsMissionIssued,child);
        child.setBackRemark(null);
        child.setParentId(limsMissionIssued.getMiId().toString());
        limsMissionIssuedChildService.saveOrUpdate(child);
        return toAjax(limsMissionIssuedService.updateById(limsMissionIssued));
    }

    /**
     * 删除项目
     */
    @PreAuthorize("@ss.hasPermi('lims:mission:issued:remove')")
    @Log(title = "项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{miIds}")
    public AjaxResult remove(@PathVariable Integer[] miIds)
    {
        return toAjax(limsMissionIssuedService.removeByIds(Arrays.asList(miIds)));
    }
    @Log(title = "项目-上传附件", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('lims:mission:issued:importAttachment')")
    @PostMapping("/importAttachment")
    public AjaxResult importAttachment(MultipartFile file, HttpServletRequest request) throws Exception
    {
        return success( limsMissionIssuedService.importAttachment(file, request.getParameter("ids")));
    }
}
